package com.cykj.eurekaribbon.controller;

import com.cykj.eurekaribbon.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;


    // 以下是四种请求方式
    @RequestMapping("/test1")
    public String getEurekaServiceInfo(){
        /**
         * exchange(url,type,paras,resutType)
         * url：请求地址
         * type：请求类型 get,post
         * paras：参数
         * resutType：返回值类型
         */
        String url="http://client/getInfo";
        HttpMethod type=HttpMethod.GET;
        RequestEntity<String> paras=null;
        ResponseEntity<String> responseEntity=restTemplate.exchange(url,type,paras,String.class);
        return responseEntity.getBody();
    }

    @RequestMapping("/test2")
    public String getString1(){
        System.out.println("test2");
        //getForObject 调用无参方法，返回结果为String的方法
        String url="http://client/getInfo";
        String res=restTemplate.getForObject(url,String.class);
        return res;
    }

    @RequestMapping("/test3")
    public String getString2(){
        //getForObject 调用有参方法，路径添加参数。返回结果为String的方法
        String url="http://client/getString?userId=sn001";
        String res=restTemplate.getForObject(url,String.class);
        return res;
    }

    @RequestMapping("/test4")
    public int getUser(){
        System.out.println("進入test4");
        //参数为实体类对象，返回结果为实体类对象
        User user=new User();
        user.setUserId(1);
        user.setUserName("zsc");
        user.setPassWord("123");
        String url="http://client/getUser";
//        User u=restTemplate.getForObject(url,User.class,user);
//        u代表生产者返回的数据
        User u =restTemplate.postForObject(url,user,User.class);
        System.out.println("url:"+url);
        System.out.println("user:"+user.getUserId());

        return 2;
    }

}
